﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
using BOHVote.Common;
using BOHVote.Entity;
 

namespace BOH.Interview
{
    public partial class interviewInfolist : System.Web.UI.Page
    {
        private BHUser user;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    user = (BHUser)Session["User"];
                    switch (user.RightType)
                    { 
                        case "S":
                            panelSubject.Visible = false;
                            //GetApplyInfo(user.RightCode);
                            break;
                        case "C":
                        case "A":
                            SubjectBindID("");
                            panelSubject.Visible = true;
                            //GetApplyInfo("-1");
                            break;
                        default:
                            break;
                    }
                }
                //SubjectBind("");
                //GetApplyInfo();
            }
        }

        public void SubjectBindID(string SubjectID)
        {
            this.drpSubject.Items.Clear();
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            if (String.IsNullOrEmpty(SubjectID))
            {
                DataSet dst = Subject.getAllASubject();
                
                foreach (DataRow dr in dst.Tables[0].Rows)
                {
                    if (dr["SubjectID"].ToString().Length > 0)
                    {
                        ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                        this.drpSubject.Items.Add(li);
                    }
                }
            }
            else
            {
                string SubjectName = Subject.getSubjectNameBySubjectID(SubjectID);
                ListItem li = new ListItem(SubjectName, SubjectID);
                this.drpSubject.Items.Add(li);
            }
        }

        public void SubjectBind(string level)
        {
            this.drpSubject.Items.Clear();
            DataSet dst;
            if (String.IsNullOrEmpty(level))
                dst = Subject.getAllASubject();
            else
                dst = Subject.getSubjectByLevel(level);
            //dst = Subject.
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                if (dr["SubjectID"].ToString().Length > 0)
                {
                    ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                    this.drpSubject.Items.Add(li);
                }
            }
        }

        public void GetApplyInfo(string subjectID)
        {
            DataSet dst = ApplyClass.dstApplysForInterview(txtApplyName.Text,"-1", "-1", "-1", "-1", "-1", "-1", subjectID, "-1", (string)Session["ApplyPeriod"]);

                BindApplyData(dst);
        }

        public void BindApplyData(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    spanTotal.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = dst.Tables[0].DefaultView;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    labTotalNo.Text = AspNetPager1.RecordCount.ToString();
                    InterviewList.DataSource = pds;
                    InterviewList.DataBind();
                }
                else
                {
                    InterviewList.DataSource = null;
                    spanTotal.Visible = false;
                    InterviewList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                }
            }
            else
            {
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                spanTotal.Visible = false;
                InterviewList.DataSource = null;
                InterviewList.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            butSearch_Click(null,null);
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(((BHUser)Session["User"]).RightCode))
                GetApplyInfo(((BHUser)Session["User"]).RightCode);
            else
            {
                if(!String.IsNullOrEmpty(drpSubject.SelectedValue))
                    GetApplyInfo(drpSubject.SelectedValue);
            }
        }

        public string ChangeDate(string strDate)
        { 
            if(!String.IsNullOrEmpty(strDate))
            {
                DateTime dt = DateTime.Parse(strDate);
                return common.ChangeDateFormatToString(dt);
            }   
            else
            {
                return strDate;
            }
        }
    }
}
